#include "HeaderRecord.h"

#include <QDebug>
#include <QDateTime>

HeaderRecord::HeaderRecord() :
    ASTM()
{
    protocolVersion = version();
}

HeaderRecord::HeaderRecord(MsgType type)
{
    protocolVersion = version();
    msgType = type;
    creatTime = QDateTime::currentDateTime().toString("yyyyMMddHHmmss");
}

QString HeaderRecord::buildRecord() const
{
    QString messageType = msgTypeList.value(msgType);
    QString record = recordTypeSymbol(RecordType::Header);

    record += FIELD;
    record += symbols;
    record += FIELD;
    record += sender;
    record += FIELD;
    record += messageType;
    record += FIELD;
    record += protocolVersion;
    record += FIELD;
    record += creatTime;
    record += CR;

    return record;
}

bool HeaderRecord::analyzeRecord(const QString &str)
{
    QStringList fields = str.split(FIELD);

    if(fields.count() < 6 || fields.value(0) != recordTypeSymbol(RecordType::Header)) {
        // qWarning() << "HeaderRecord: Syntax Error!";
        return false;
    }

    symbols = fields[1];
    sender = fields[2];
    if(msgTypeList.indexOf(fields[3]) >= 0) {
        msgType = static_cast<MsgType>(msgTypeList.indexOf(fields[3]));
    }

    protocolVersion = fields[4];
    creatTime = fields[5];

    return true;
}

QString HeaderRecord::msgTypeSymbol() const
{
    return msgTypeList.value(msgType);
}
